c#菜鸟问题List<T>泛型类的值的比较问题

来源:百度知道 编辑:UC知道 时间:2024/05/27 23:26:30
问一个.Net的问题(语言C#),List<T>泛型类,
我写了这么个测试类,
class A
{
public int i;
public int j;
public A(int a,int b)
{
i=a;
j=b;
}
}
////////////////////////////////
然后运行这样的代码
List<A> test=new List<A>();
test.Add(new A(0,0));
A temp=new A(0,0);
然后比较 test[0]==temp居然是false!!!
谁知道怎么回事啊?
由于这个原因,IndexOf()也一律返回-1,很郁闷啊~~

private class A : IEquatable<A>
{
→→public int i;
→→public int j;

→→public A(int a, int b)
→→{
→→→→i = a;
→→→→j = b;
→→}

→→public bool Equals(A other)
→→{
→→→→if (ReferenceEquals(null, other)) return false;
→→→→if (ReferenceEquals(this, other)) return true;
→→→→return other.i == i && other.j == j;
→→}

→→public override bool Equals(object obj)
→→{
→→→→if (ReferenceEquals(null, obj)) return false;
→→→→if (ReferenceEquals(this, obj)) return true;
→→→→if (obj.GetType() != typeof (A)) return false;
→→→→return Equals((A) obj);
→→}

→→public override int GetHashCode()
→→{
→→→→unchecked
→→→→{
→→→→→→return (i*397) ^ j;
→→→→}
→→}

→→public static bool operator ==(A left, A right)
→→{
→→→→return Equals(left, right);
→→}

→→public static bool operator !=(A left, A right)
→→{
→→→→retur